﻿using System;
using System.Configuration;
//如果是winform或console等c/s程序，需要设置一下。
//具体步骤：右键log4net.config文件-属性-复制到输出目录：始终复制。目的是为了每次启动时能够找到这个config文件
using System.Reflection;

[assembly: log4net.Config.XmlConfigurator(Watch = true, ConfigFile = "log4net.config")]
namespace MyLog4net
{
    /// <summary>
    /// 日志记录类
    /// </summary>
    public class MyLogger
    {
       static object log4NetLocalInfo = ConfigurationSettings.AppSettings["log4NetLocalInfo"];
       static object log4NetDBType = ConfigurationSettings.AppSettings["log4NetDBType"];
       static object log4NetLocalError = ConfigurationSettings.AppSettings["log4NetLocalError"];
        #region 记录日志
        /// <summary>
        /// 记录日志
        /// </summary>
        /// <param name="legLevel">日志级别</param>
        /// <param name="msg">日志内容</param>
        public static void LoggerMsg(string msg)
        {
            log4net.ILog logDB,logLocalInfo;

            if (log4NetLocalInfo!=null)
            {
                logLocalInfo= log4net.LogManager.GetLogger(log4NetLocalInfo.ToString());
                logLocalInfo.Info(msg);
            }
            //if (log4NetLocalError != null)
            //{
            //    logLocalError = log4net.LogManager.GetLogger(log4NetLocalError.ToString());
            //    logLocalError.Info(msg);
            //}
            if (log4NetDBType != null)
            {
                logDB = log4net.LogManager.GetLogger(log4NetDBType.ToString());
                logDB.Info(msg);
            }
            //MethodBase.GetCurrentMethod().DeclaringType 返回：命名空间名+类名
            //return log4net.LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
           // log4net.ILog log = log4net.LogManager.GetLogger("AdoNetAppender_SqlServer");
           

        }
        #endregion

        #region 记录日志
        /// <summary>
        /// 记录日志
        /// </summary>
        /// <param name="legLevel">日志级别</param>
        /// <param name="msg">日志内容</param>
        public static void LoggerErrMsg(string msg, Exception ex)
        {
            log4net.ILog logDB, logLocalError;
            //if (log4NetLocalInfo != null)
            //{
            //    logLocalInfo = log4net.LogManager.GetLogger(log4NetLocalInfo.ToString());
            //    logLocalInfo.Error(msg,ex);
            //}
            if (log4NetLocalError != null)
            {
                logLocalError = log4net.LogManager.GetLogger(log4NetLocalError.ToString());
                logLocalError.Error(msg, ex);
            }
            if (log4NetDBType != null)
            {
                logDB = log4net.LogManager.GetLogger(log4NetDBType.ToString());
                logDB.Error(msg, ex);
            }

        }
        #endregion


    }

}

public enum LogType
{
    AdoNetAppender_SqlServer,
    AdoNetAppender_Oracle,
    AdoNetAppender_Access,
    Locallogererror,
    Locallogerrinfo
}


